package com.nfit.web.transaction;

import com.nfit.entity.Transactions;
import com.nfit.entity.vo.ResponeVo;
import com.nfit.service.TransactionsService;
import org.apache.commons.math3.stat.regression.SimpleRegression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

import static com.nfit.util.TypeUtils.convertArrayToMap;

@RestController
public class TransactionsController {
    @Autowired
    private TransactionsService service;

    @RequestMapping("/transactions/all")
    public ResponeVo all(String userEmail) {
        List<Transactions> transactions = service.selectAll(userEmail);
        if (transactions == null) {
            return ResponeVo.fail("暂无数据");
        }
        return ResponeVo.success(transactions);
    }

    @RequestMapping("transactions/insert")
    public ResponeVo insert(@RequestBody Transactions transaction) {
        int result = service.insert(transaction);
        if (result == 0) {
            return ResponeVo.fail("添加失败");
        }
        return ResponeVo.success(transaction);
    }

    @RequestMapping("transactions/selectTransactionById")
    public ResponeVo selectTransactionById(String transactionId, String userId) {
        Transactions transaction = service.selectTransactionById(transactionId, userId);
        if (transaction == null) {
            return ResponeVo.fail("暂无数据");
        }
        return ResponeVo.success(transaction);
    }

    @RequestMapping("transactions/update")
    public ResponeVo updateTransaction(@RequestBody Transactions transaction) {
        int result = service.updateTransaction(transaction);
        if (result == 0){
            return ResponeVo.fail("更新失败");
        }
        return ResponeVo.success(transaction);
    }

    @RequestMapping("transactions/delete")
    public ResponeVo deleteTransactionById(String transactionId) {
        int result = service.deleteTransactionById(transactionId);
        if (result == 0) {
            return ResponeVo.fail("删除失败");
        }
        return ResponeVo.success("删除成功");
    }

    @RequestMapping("/transactions/selectCurrencyTransactions")
    public ResponeVo selectCurrencyTransactions(String userEmail) {
        List<Transactions> transactions = service.selectAll(userEmail);
        if (transactions == null) {
            return ResponeVo.fail("暂无数据");
        }
        for (Transactions transaction : transactions) {
            transaction.setCurrencyCode(transaction.getCurrencyCode().substring(0, transaction.getCurrencyCode().length() - 4));
        }
        return ResponeVo.success(transactions);
    }

    @RequestMapping("/transactions/selectAmounts")
    public ResponeVo selectAmounts(String userId,String transactionType) {
        double[][] amounts = service.selectAmounts(userId,transactionType);

        // 创建线性回归对象
        SimpleRegression regression = new SimpleRegression();

        // 添加数据到回归模型（使用年份作为自变量）
        for (double[] row : amounts) {
            regression.addData(row[0], row[1]); // (年份, 广告支出)
        }

        // 获取回归参数
        double slope = regression.getSlope();
        double intercept = regression.getIntercept();
        // 预测2024年的广告支出
        double futureYear = LocalDate.now().getYear() + 1;

        System.out.println(futureYear);
        double predictedAdvertising = regression.predict(futureYear);
        Map<Double, Double> result = convertArrayToMap(amounts);
        result.put(futureYear, predictedAdvertising);
        return ResponeVo.success(result);
    }
}
